package com.hundsun.yr.universal.library.widget;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.hundsun.yr.universal.library.R;
import com.hundsun.yr.universal.library.common.CPResourceUtil;


/**
 * @Title: HSSwipeRefreshLayout.java 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @date 2015-10-19 上午10:27:41 
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com 
 * @replace author:
 * @replace date:
 * @version V1.0   
 */
public class HSSwipeRefreshLayout extends SwipeRefreshLayout {

    public HSSwipeRefreshLayout(Context context) {
        this(context,null);
    }

    public HSSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        init(context,attrs);
    }
    
    private void init(Context context, AttributeSet attrs){
    	
//    	int gplus_color_1=CPResourceUtil.getColorId(context, "gplus_color_1");
//    	int gplus_color_2=CPResourceUtil.getColorId(context, "gplus_color_2");
//    	int gplus_color_3=CPResourceUtil.getColorId(context, "gplus_color_3");
//    	int gplus_color_4=CPResourceUtil.getColorId(context, "gplus_color_4");
//    	
//    	if(gplus_color_1!=0&&gplus_color_2!=0&&gplus_color_3!=0&&gplus_color_4!=0){
//    		setColorSchemeColors(
//                    getResources().getColor(gplus_color_1),
//                    getResources().getColor(gplus_color_2),
//                    getResources().getColor(gplus_color_3),
//                    getResources().getColor(gplus_color_4));
//    	}else{
//    		setColorSchemeColors(
//                    getResources().getColor(R.attr.theme_color),
//                    getResources().getColor(R.attr.theme_color),
//                    getResources().getColor(R.attr.theme_color),
//                    getResources().getColor(R.attr.theme_color));
//    	}
    	
    	
    	TypedValue typedValue = new TypedValue();
    	context.getTheme().resolveAttribute(R.attr.theme_color, typedValue, true);
    	int gplus_color = typedValue.resourceId;
    	setColorSchemeColors(
    			getResources().getColor(gplus_color),
    			getResources().getColor(gplus_color),
    			getResources().getColor(gplus_color),
    			getResources().getColor(gplus_color));
    }

    private float xDistance, yDistance, xLast, yLast;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;

                if (xDistance > yDistance) {
                    return false;
                }
        }

        return super.onInterceptTouchEvent(ev);
    }

    private OnScrollViewScrollListener mOnScrollViewScrollListener = null;

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (mOnScrollViewScrollListener != null) {
            mOnScrollViewScrollListener.onScrollChanged(x, y, oldx, oldy);
        }
    }

    public void setOnScrollListener(OnScrollViewScrollListener listener) {
        this.mOnScrollViewScrollListener = listener;
    }

    public interface OnScrollViewScrollListener {
        public void onScrollChanged(int x, int y, int oldX, int oldY);
    }
    
    /**
     * 自动刷新
     */
    public void autoRefresh() {
        try {
            Field mCircleView = SwipeRefreshLayout.class.getDeclaredField("mCircleView");
            mCircleView.setAccessible(true);
            View progress = (View) mCircleView.get(this);
            progress.setVisibility(VISIBLE);
 
            Method setRefreshing = SwipeRefreshLayout.class.getDeclaredMethod("setRefreshing", boolean.class, boolean.class);
            setRefreshing.setAccessible(true);
            setRefreshing.invoke(this, true, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
